<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Jul 17, 2013
 * Time: 11:37:35 AM
 * To change this template use File | Settings | File Templates.
 */

class TldHolder {
    private $db;
    private $id;
    private $code;
    private $lotId;


    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
    }

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function updateCard($id, $passTest, $qaCard, $dosimeter) {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $card = new TldCardAR();
            $update = false;
            foreach ($id as $value) {
                $this->id = $value;
                if ($this->id != '' AND $this->id != NULL) {
                    $card->Load("id=$this->id");
                    $card->update_user_id = $_SESSION['user_id'];
                    $card->is_pass_test = $passTest[$value] == 1 ? 1 : 0;
                    $card->is_dosimeter = $dosimeter[$value] == 1 ? 1 : 0;
                    $card->is_qa_card = $qaCard[$value] == 1 ? 1 : 0;
                    $j = $card->save();
                    if ($j == 1) {
                        $update = true;
                    }
                }
            }

        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());

        }

        if ($update) {
            $i = 1;
        } else {
            $i = -1;
        }

        return $i;
    }

    function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $card = new TldHolderAR();
            if ($this->id != '' AND $this->id != NULL) {
                $card->Load("id=$this->id");
            } else {
                $card->create_time = date('Y-m-d H:i:s', time());
                $card->create_user_id = $_SESSION['user_id'];
                $card->is_pass_test = 0;
                $card->is_dosimeter = 0;
                $card->is_qa_card = 0;
                $card->is_delete = 0;

            }
            $card->lot_id = $this->lotId;
            $card->code = $this->code;
            $i = $card->save();
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getTldHolderGrid($lotId, $code) {

        $sql = "SELECT  * FROM aea_holder Where is_delete='0'";
        if ($lotId != '')
            $sql .= " and lot_id = '$lotId'";
        if ($code != '')
            $sql .= " and code like '$code%'";

        $resAll = $this->db->Execute($sql);
        $totalRows = $resAll->RecordCount();
        if ($totalRows > 0) {
            $innerHtml = "<table class='gradienttable'><tr><th width='50px'>id</th><th width='250px'>Code</th><th width='150px'>Pass Performance Test</th><th width='150px'>Dosimeter </th> <th width='150px'>QA card </th></tr>";
            while (!$resAll->EOF) {

                $innerHtml .= "<tr> <input type='hidden' id='id'  name='id[]' value='" . $resAll->fields['id'] . "' />
                    <td>" . $resAll->fields['id'] . "</td>
                   <td align='center'>" . $resAll->fields['code'] . "</td>
                   <td align='center'><input type='checkbox'  value='1' id='performanceTest" . $resAll->fields['id'] . "' name='performanceTest[" . $resAll->fields['id'] . "]' ";
                if ($resAll->fields['is_pass_test'] == 1) {
                    $innerHtml .= " checked";
                }
                $innerHtml .= "  > </td>
                   <td align='center'><input type='checkbox' value='1' id='dosimeter" . $resAll->fields['id'] . "' name='dosimeter[" . $resAll->fields['id'] . "]' ";
                if ($resAll->fields['is_dosimeter'] == 1) {
                    $innerHtml .= " checked";
                }
                $innerHtml .= "  > </td>
                   <td align='center'><input type='checkbox' value='1' id='qaCard" . $resAll->fields['id'] . "' name='qa[" . $resAll->fields['id'] . "]' ";
                if ($resAll->fields['is_qa_card'] == 1) {
                    $innerHtml .= " checked";
                }
                $innerHtml .= " value='" . $resAll->fields['is_qa_card'] . "' > </td>
                   </tr>";
                $resAll->MoveNext();
            }
            $innerHtml .= "</table> <input type='hidden' id='total'  name='total' value='" . $totalRows . "' />";

        } else {
            $innerHtml = "No Records Found !";
        }
        $data = array('tableHtml' => $innerHtml);
        return $data;
    }
}

require_once('libraries/adodb5/adodb-active-record.inc.php');
class TldHolderAR extends ADOdb_Active_Record {
    var $_table = 'aea_holder';

}